home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swags_z.zip / TIMING.SWG / 0025_ASM High Resolution Timer.pas < prev    next >
Pascal/Delphi Source File  |  1995-03-03  |  3KB  |  147 lines

  1. {
  2. > Does anybody knows if there's a timer function in turbo pascal (version 5) or
  3. > turbo pascal for windows in which the user could start and stop a timer using
  4. > this function.
  5.  
  6. Here is the unit I wrote for high precision timing.  Is can be used for
  7. timing events of up to 54 milliseconds duration accurate to a few microseconds.
  8. Hope it helps.
  9.  
  10. From: JOHARROW@homeloan.demon.co.uk (John O'Harrow)
  11. }
  12. UNIT Timer;
  13.  
  14. (*High Precision Timer Library for Borland Pascal, for timing operations 
  15.   of less than 54 milliseconds duration, accurate to a few microseconds.
  16.   Based on code originally written by Michael Abrash for his book "Zen of
  17.   Assembly language", and on Kendall Bennett's subsequent enhancements.*)
  18.  
  19. INTERFACE
  20.  
  21.   PROCEDURE StartTimer;          {Start the High-Precision Timer   }
  22.   PROCEDURE StopTimer ;          {Stop the High-Precision Timer    }
  23.  
  24.   FUNCTION  TimeTaken : LongInt; {Return Time Taken in Microseconds}
  25.                                  {Returns -1 if Time Taken > 54 mS }
  26. IMPLEMENTATION
  27.  
  28. VAR
  29.   Flags    : Byte;
  30.   Overflow : Byte;
  31.   Counter  : Word;
  32.   RefCount : Word;
  33.  
  34.   PROCEDURE RefOn; ASSEMBLER;
  35.   ASM
  36.     mov  al,00110100b
  37.     out  43h,al
  38.     db   $EB,0,$EB,0,$EB,0
  39.     sub  al,al
  40.     out  40h,al
  41.     db   $EB,0,$EB,0,$EB,0
  42.     out  40h,al
  43.   END;
  44.  
  45.   PROCEDURE RefOff; ASSEMBLER;
  46.   ASM
  47.     mov  al,0
  48.     out  43h,al
  49.     db   $EB,0,$EB,0,$EB,0
  50.     in   al,40h
  51.     db   $EB,0,$EB,0,$EB,0
  52.     mov  ah,al
  53.     in   al,40h
  54.     xchg ah,al
  55.     neg  ax
  56.     add  [RefCount],ax
  57.   END;
  58.  
  59.   PROCEDURE StartTimer; ASSEMBLER;
  60.   ASM
  61.     pushf
  62.     pop  ax
  63.     mov  [Flags],ah
  64.     and  ah,0fdh
  65.     push ax
  66.     sti
  67.     mov  al,00110100b
  68.     out  43h,al
  69.     db   $EB,0,$EB,0,$EB,0
  70.     sub  al,al
  71.     out  40h,al
  72.     db   $EB,0,$EB,0,$EB,0
  73.     out  40h,al
  74.     mov  cx,10
  75.   @@Delay:
  76.     Loop @@Delay
  77.     cli
  78.     mov  al,00110100b
  79.     out  43h,al
  80.     db   $EB,0,$EB,0,$EB,0
  81.     sub  al,al
  82.     out  40h,al
  83.     db   $EB,0,$EB,0,$EB,0
  84.     out  40h,al
  85.     popf
  86.   END;
  87.  
  88.   PROCEDURE StopTimer; ASSEMBLER;
  89.   ASM
  90.     pushf
  91.     mov  al,0
  92.     out  43h,al
  93.     mov  al,00001010b
  94.     out  20h,al
  95.     db   $EB,0,$EB,0,$EB,0
  96.     in   al,20h
  97.     and  al,1
  98.     mov  [Overflow],al
  99.     sti
  100.     in   al,40h
  101.     db   $EB,0,$EB,0,$EB,0
  102.     mov  ah,al
  103.     in   al,40h
  104.     xchg ah,al
  105.     neg  ax
  106.     mov  [Counter ],ax
  107.     mov  [RefCount],0
  108.     mov  cx,16
  109.     cli
  110.   @@Loop:
  111.     call RefOn
  112.     call RefOff
  113.     loop @@Loop
  114.     sti
  115.     add  [RefCount],8
  116.     mov  cl,4
  117.     shr  [RefCount],cl
  118.     pop  ax
  119.     mov  ch,[Flags]
  120.     and  ch,2
  121.     and  ah,0fdh
  122.     or   ah,ch
  123.     push ax
  124.     popf
  125.   END;
  126.  
  127.   FUNCTION TimeTaken : LongInt; ASSEMBLER;
  128.   ASM
  129.     cmp  [Overflow],0
  130.     jz   @@Good
  131.     mov  ax,0FFFFh
  132.     mov  dx,0FFFFh
  133.     jmp  @@Done
  134.   @@Good:
  135.     mov  ax,[Counter ]
  136.     sub  ax,[RefCount]
  137.     mov  dx,8381
  138.     mul  dx
  139.     mov  bx,10000
  140.     div  bx
  141.     xor  dx,dx
  142.   @@Done:
  143.   END;
  144.  
  145. END.
  146.  
  147.